/*
 * @author ${user}
 * @date ${year}-${month}-${day}
 */

package net.xdclass.component;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.enums.ProductOrderPayTypeEnum;
import net.xdclass.vo.PayInfoVO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class PayFactory {

	private final Map<String,PayStrategy> map = new ConcurrentHashMap();

	public PayFactory(List<PayStrategy> payStrategyList){
		for(PayStrategy payStrategy:payStrategyList){
			map.put(payStrategy.getType(),payStrategy);
		}}

		private PayStrategy getPayStrategy(PayInfoVO payInfoVO){
			String payType = payInfoVO.getPayType();
			PayStrategy payStrategy = map.get(payType);
			if (payStrategy != null){
				return payStrategy;
			}
			return  null;
		}

	public String unifiedOrder(PayInfoVO payInfoVO){
		String result = "";
		PayStrategy payStrategy = getPayStrategy(payInfoVO);
		if (payStrategy != null){
		PayStrategyContext payStrategyContext = new PayStrategyContext(payStrategy);
	    result = payStrategyContext.unifiedOrder(payInfoVO); }
	    else{
	    	log.error("不支持的支付方式");
		}
		return result;
	}

	/**
	 * 退款接口
	 * @param payInfoVO
	 * @return
	 */
	public String refund(PayInfoVO payInfoVO){
		String result = "";
		PayStrategy payStrategy = getPayStrategy(payInfoVO);
		if (payStrategy != null){
			PayStrategyContext payStrategyContext = new PayStrategyContext(payStrategy);
			result = payStrategyContext.refund(payInfoVO); }
		else{
			log.error("不支持的支付方式");
		}
		return result;
	}


	/**
	 * 查询支付状态
	 * @param payInfoVO
	 * @return
	 */
	public String queryPayStatus(PayInfoVO payInfoVO){
		String result = "";
		PayStrategy payStrategy = getPayStrategy(payInfoVO);
		if (payStrategy != null){
			PayStrategyContext payStrategyContext = new PayStrategyContext(payStrategy);
			result = payStrategyContext.queryPayStatus(payInfoVO); }
		else{
			log.error("不支持的支付方式");
		}
		return result;
	}


	/**
	 * 关闭订单
	 * @param payInfoVO
	 * @return
	 */
	public String closeOrder(PayInfoVO payInfoVO){
		String result = "";
		PayStrategy payStrategy = getPayStrategy(payInfoVO);
		if (payStrategy != null){
			PayStrategyContext payStrategyContext = new PayStrategyContext(payStrategy);
			result = payStrategyContext.closeOrder(payInfoVO); }
		else{
			log.error("不支持的支付方式");
		}
		return result;
	}

}
